package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.domain.Pomr;
import com.service.UserService;
import com.utils.Page;
import com.utils.Ret;

@Controller
public class demo {
	@Autowired  
	UserService userServiece;
	
	@RequestMapping("index.do")
	public String testParms(){
		System.out.println("====");
		return "demo/index";
	}
	@RequestMapping("test.do")
	public void test(){
		System.out.println("====");
	}
	
	@RequestMapping("user.do")
	public String getUser(@RequestParam(value="uid",defaultValue="1")String uid,
			@RequestParam(value="uname",defaultValue="a")String uname,
			@RequestParam(value="age",defaultValue="1")int age,
			@RequestParam(value="pageNum", defaultValue="1")int pageNum,
			@RequestParam(value="pageSize",defaultValue="10")int pageSize, Model model){
		Map<String,Object> params = new HashMap<String,Object>();
		
		params.put("uid", uid);
		params.put("uname", uname);
		params.put("age", age);
		params.put("pageBegin", (pageNum-1)*pageSize);
		params.put("pageSize", pageSize);
		params.put("count", userServiece.getCountOfUser());
		
		model.addAttribute("userList", userServiece.getUser(params));
		model.addAttribute("params",params);
		return "demo/user";
	}
	
	@RequestMapping("toAdd.do")
	public String toAdd(){
		return "demo/add";
	}
	
	@RequestMapping("addUser.do")
	public void addUser(Pomr user,HttpServletRequest request,PrintWriter out){
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("uname", request.getParameter("uname"));
		params.put("pwd", request.getParameter("pwd"));
		params.put("age",request.getParameter("age"));
		
		System.out.println(user.toString());
		
		try{
		//	userServiece.addUser(user);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		Ret ret = new Ret();
		ret.setStatusCode("200");
		ret.setMessage("asdf");
		out.print(ret.toJsonString());
		out.flush();
		out.close();
	}
	
	@RequestMapping("deleteUser.do")
	public void deleteUser(@RequestParam("ids") int uid){
		System.out.println(uid);
		userServiece.deleteUser(uid);
	}

}
